package controller

import (
	"github.com/gin-gonic/gin"
	"log"
	"strconv"
	"wjp/ginandvue/model"
	"wjp/ginandvue/repository"
	"wjp/ginandvue/response"
	"wjp/ginandvue/vo"
)

type ICategoryController interface {
	RestController
}

type CategoryController struct {
	//DB *gorm.DB
	Repository repository.ICategoryRepository
}

func NewCategoryController() ICategoryController{
		//db := common.Db
		//db.AutoMigrate(&model.Category{})
		//
		//return CategoryController{DB:db}
	categoryController := CategoryController{Repository:repository.NewCategoryRepository()}
	categoryController.Repository.(repository.CategoryRepository).DB.AutoMigrate(model.Post{})
	return categoryController
}
func (c CategoryController) Create(ctx *gin.Context){
	//var requestCategory vo.CreateCategoryRequest
	//if err := ctx.ShouldBind(&requestCategory);err != nil{
	//	response.Fail(ctx,nil,"分类名称必填")
	//	return
	//}
	////ctx.Bind(&requestCategory)
	////
	////if requestCategory.Name == ""{
	////	response.Fail(ctx,nil,"分类名称必填")
	////	return
	////}
	//category := model.Category{Name:requestCategory.Name}
	//c.DB.Create(&category)
	//response.Success(ctx,gin.H{"category":requestCategory},"")

	// 获取参数
	var requestCategory vo.CreateCategoryRequest
	if err := ctx.ShouldBind(&requestCategory); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil,"数据验证错误" )
		return
	}
	category, err := c.Repository.Create(requestCategory.Name)
	if err != nil  {
		response.Fail(ctx, nil,"数据验证错误")
		return
	}

	response.Success(ctx, gin.H{"category": category}, "创建成功")
}
func (c CategoryController) Update(ctx *gin.Context){
	//方式一
	//var requestCategory model.Category
	//ctx.Bind(&requestCategory)
	//
	//if requestCategory.Name == ""{
	//	response.Fail(ctx,nil,"分类名称必填")
	//	return
	//}

	//方式二
	//var requestCategory vo.CreateCategoryRequest
	//if err := ctx.ShouldBind(&requestCategory);err != nil{
	//	response.Fail(ctx,nil,"分类名称必填")
	//	return
	//}
	////获取path中的参数
	//categoryId,_ := strconv.Atoi(ctx.Params.ByName("id"))
	//
	//var updateCategory  model.Category
	//if c.DB.First(&updateCategory,categoryId).RecordNotFound(){
	//	response.Fail(ctx,nil,"分类不存在")
	//	return
	//}
	//
	//c.DB.Model(&updateCategory).Update("name",requestCategory.Name)
	//response.Success(ctx,gin.H{"category":requestCategory},"修改成功")

	// 获取body 参数
	var requestCategory vo.CreateCategoryRequest
	if err := ctx.ShouldBind(&requestCategory); err != nil {
		log.Println(err.Error())
		response.Fail(ctx, nil,"数据验证错误" )
		return
	}

	// 获取 path 参数
	categoryId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	var updateCategory *model.Category
	category, err := c.Repository.SelectById(categoryId)
	if err != nil {
		panic(err)
	}
	updateCategory, err = c.Repository.Update(*category, requestCategory.Name)
	if err != nil {
		panic(err)
	}
	response.Success(ctx, gin.H{"category": updateCategory}, "更新成功")
}
func (c CategoryController) Show(ctx *gin.Context){
	//获取path中的参数
	//categoryId,_ := strconv.Atoi(ctx.Params.ByName("id"))
	//
	//var category  model.Category
	//if c.DB.First(&category,categoryId).RecordNotFound(){
	//	response.Fail(ctx,nil,"分类不存在")
	//	return
	//}
	// 获取 path 参数
	categoryId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	category, err := c.Repository.SelectById(categoryId)

	if err != nil {
		panic(err)
	}

	response.Success(ctx,gin.H{"category":category},"")
}
func (c CategoryController) Delete(ctx *gin.Context){
	//获取path中的参数
	//categoryId,_ := strconv.Atoi(ctx.Params.ByName("id"))
	//if err := c.DB.Delete(model.Category{},categoryId).Error;err !=nil{
	//	response.Fail(ctx,nil,"删除失败")
	//	return
	//}

	// 获取 path 参数
	categoryId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	c.Repository.DeleteById(categoryId)

	response.Success(ctx,nil,"删除成功")
}